function dataviewergui()    
%% init
dvh.hf=figure(1001);

% next four lines make the figure stay on top
warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
j = get(dvh.hf,'JavaFrame');
drawnow %importatnt for the next step
w = javaMethodEDT('getWindow',j.fFigureClient);
javaMethodEDT('setAlwaysOnTop',w,true) 

clf

mon_pos=get(0,'MonitorPositions');
set(dvh.hf,'color',get(0,'defaultUicontrolBackgroundColor'),'position',[mon_pos(1,1)+round(0.75*(mon_pos(1,3)-mon_pos(1,1))) 150 250 550],'menubar','none');

[xls_num,xls_txt] = xlsread(ImageAnalysis.Settings.ExcelFile);
adata_dir = ImageAnalysis.Settings.AdataDir;
assignin('base','adata_dir',adata_dir);

lb1=0.1; %position left
%lb2=0.375;
lb3=0.675; %position 2nd column
w0=0.825; % full width
w1=0.525; % width col 1
w2=0.25; % width col 2
h1=0.05; % controll hight (min. 0.04 usefull)
h2=0.01; %row distance

h_ind=16;
dvh.mouse_select=uicontrol('style','popupmenu',...
    'string','-',...
    'value',1,...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w0 h1]);

h_ind=15;
dvh.date_select=uicontrol('style','popupmenu',...
    'string','-',...
    'value',1,...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w0 h1]);

h_ind=14;
dvh.exp_select=uicontrol('style','popupmenu',...
    'string','-',...
    'value',1,...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w0 h1]);


h_ind=9;
% Create the button group.
dvh.bg = uibuttongroup('visible','on','Position',[lb1 (h1+h2)*h_ind-h1 w0 5*h1]);
% Create three radio buttons in the button group.
dvh.r0 = uicontrol('Style','Radio','String','load stacks','units','normalized',...
    'pos',[lb1 (h1*2+h2)*(h_ind-1)-h1 w0 3*h1],'parent',dvh.bg,'HandleVisibility','on');
dvh.r1 = uicontrol('Style','Radio','String','load & register data','units','normalized',...
    'pos',[lb1 (h1*2+h2)*(h_ind-3)-h1 w0 3*h1],'parent',dvh.bg,'HandleVisibility','off');
dvh.r2 = uicontrol('Style','Radio','String','load, register & roi','units','normalized',...
    'pos',[lb1 (h1*2+h2)*(h_ind-5)-h1 w0 3*h1],'parent',dvh.bg,'HandleVisibility','off');
dvh.r3 = uicontrol('Style','Radio','String','load, register, stim sync & roi','units','normalized',...
    'pos',[lb1 (h1*2+h2)*(h_ind-7)-h1 w0 3*h1],'parent',dvh.bg,'HandleVisibility','off');
% Initialize some button group properties. 
set(dvh.bg,'SelectedObject',[]);  % No selection
%set(dvh.bg,'Visible','on');

h_ind=7;
dvh.process=uicontrol('style','pushbutton',...
    'string','process',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'HorizontalAlignment','left',...
    'callback',{@process_callback,xls_num,xls_txt,dvh,adata_dir});

h_ind=8;
dvh.check=uicontrol('style','pushbutton',...
    'string','check',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'HorizontalAlignment','left',...
    'callback',{@check_callback,xls_num,xls_txt,dvh,adata_dir});


h_ind=5;
dvh.load_tiff=uicontrol('style','checkbox',...
    'string','load images',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','r',...
    'HorizontalAlignment','left');

h_ind=6;
dvh.load_eye=uicontrol('style','checkbox',...
    'string','load eye(s)',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','r',...
    'HorizontalAlignment','left');

h_ind=4;
dvh.load=uicontrol('style','pushbutton',...
    'string','load',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback',{@load_callback,xls_num,xls_txt,dvh,adata_dir});

h_ind=3;
dvh.select_rois=uicontrol('style','pushbutton',...
    'string','select ROIs',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left');

h_ind=2;
%% save A-data function
dvh.save_adata=uicontrol('style','pushbutton',...
    'string','save A-data',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback','evalin(''base'',''saveadata(adata_dir,ROIs,bv,np,template,dx,dy,aux_files,stim_files,fnames,nbr_frames,mouse_id,date_id,act_map,channelFiles)'');');
%%
h_ind=1;
dvh.plot_data=uicontrol('style','pushbutton',...
    'string','plot it',...
    'units','normalized',...
    'position',[lb1 (h1+h2)*h_ind-h1 w1 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback',{@plot_callback,xls_num,xls_txt,dvh,adata_dir});

h_ind=5;
dvh.preview_adata=uicontrol('style','pushbutton',...
    'string','get adata',...
    'units','normalized',...
    'position',[lb3 (h1+h2)*h_ind-h1 w2 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback',{@preview_adata_callback,xls_num,xls_txt,dvh,adata_dir});

h_ind=4;
dvh.register_entire_dir=uicontrol('style','pushbutton',...
    'string','reg. dir',...
    'units','normalized',...
    'position',[lb3 (h1+h2)*h_ind-h1 w2 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback',{@register_entire_dir_callback,xls_num,xls_txt,dvh,adata_dir});

h_ind=3;
dvh.register_entire_dir=uicontrol('style','pushbutton',...
    'string','reg. m',...
    'units','normalized',...
    'position',[lb3 (h1+h2)*h_ind-h1 w2 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback',{@register_multiple,xls_num,xls_txt,dvh,adata_dir});

h_ind=2;
dvh.view_tif=uicontrol('style','pushbutton',...
    'string','view tif',...
    'units','normalized',...
    'position',[lb3 (h1+h2)*h_ind-h1 w2 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback','evalin(''base'',''view_tiff_stack(data)'');');

h_ind=1;
dvh.view_eye=uicontrol('style','pushbutton',...
    'string','view eye',...
    'units','normalized',...
    'position',[lb3 (h1+h2)*h_ind-h1 w2 h1],...
    'BackgroundColor',[1 1 1]*0.1,...
    'ForegroundColor','g',...
    'HorizontalAlignment','left',...
    'callback','evalin(''base'',''view_eye_stack(idata,imeta_data)'');');



%1;
[dateIDs,~,ExpStrs,~,mouseIDs]=get_menu_data(xls_num,xls_txt,dvh);
set(dvh.mouse_select,'string',mouseIDs,'value',1);
set(dvh.date_select,'string',dateIDs,'value',1);
set(dvh.exp_select,'string',ExpStrs,'value',1);
    
set(dvh.mouse_select,'callback',{@mouse_select_callback,dvh,xls_num,xls_txt});
set(dvh.date_select,'callback',{@date_select_callback,dvh,xls_num,xls_txt});
set(dvh.load_tiff,'callback',{@load_tiff_callback,dvh});
set(dvh.load_eye,'callback',{@load_eye_callback,dvh});
set(dvh.select_rois,'callback',{@select_rois_callback,xls_num,xls_txt,dvh,adata_dir});

function mouse_select_callback(e,h,dvh,xls_num,xls_txt)
[dateIDs,~,~,~]=get_menu_data(xls_num,xls_txt,dvh);
set(dvh.date_select,'value',1,'string',dateIDs);
date_select_callback(e,h,dvh,xls_num,xls_txt)

function date_select_callback(e,h,dvh,xls_num,xls_txt)
[~,~,ExpStrs,~]=get_menu_data(xls_num,xls_txt,dvh);
set(dvh.exp_select,'value',1,'string',ExpStrs);

function load_tiff_callback(e,h,dvh)
on_off_color='rg';
set(dvh.load_tiff,'foregroundcolor',on_off_color(get(dvh.load_tiff,'value')+1));

function load_eye_callback(e,h,dvh)
on_off_color='rg';
set(dvh.load_eye,'foregroundcolor',on_off_color(get(dvh.load_eye,'value')+1));


function load_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
%% load function
[~,ExpIDs,~,same_site_as_ID]=get_menu_data(xls_num,xls_txt,dvh);
disp(['Loading Exp --- ' num2str(ExpIDs(get(dvh.exp_select,'value'))) ' --- ' datestr(now)])
disp('clearing data to prevent mismatch!');
evalin('base','clear data');
loaddata(ExpIDs(get(dvh.exp_select,'value')),adata_dir,get(dvh.load_tiff,'value'),get(dvh.load_eye,'value'),0,same_site_as_ID(get(dvh.exp_select,'value')));

function process_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
%% process function
[~,ExpIDs,~,same_site_as_ID]=get_menu_data(xls_num,xls_txt,dvh);

if get(dvh.r0,'value')
    loadExp(ExpIDs(get(dvh.exp_select,'value')));
elseif get(dvh.r1,'value')
    regdata(ExpIDs(get(dvh.exp_select,'value')));
elseif get(dvh.r2,'value')
    regdata(ExpIDs(get(dvh.exp_select,'value')));
    evalin('base','find_cells_gui(template,ROIs,[0 0 0],act_map)');
elseif get(dvh.r3,'value')
    regdata(ExpIDs(get(dvh.exp_select,'value')));
    getstimdata(ExpIDs(get(dvh.exp_select,'value')));
    evalin('base','find_cells_gui(template,ROIs,[0 0 0],act_map)');
end

function check_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
%% check function
[~,ExpIDs,~,~]=get_menu_data(xls_num,xls_txt,dvh);
checkdata(ExpIDs(get(dvh.exp_select,'value')));


function select_rois_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
%% select ROIs function
[~,ExpIDs,~,~,~,~]=get_menu_data(xls_num,xls_txt,dvh);
%assignin('base','ExpGroup',ExpGroup);
%[ofile_ID,omouse_ID,odate_ID]=find_adata_file(same_site_as_ID(get(dvh.exp_select,'value')),adata_dir);
adata_dir = ImageAnalysis.Settings.AdataDir;
[file_ID,mouse_ID,date_ID]=find_adata_file(ExpIDs(get(dvh.exp_select,'value')),adata_dir);
%curr=load([adata_dir mouse_ID '\' date_ID '\' file_ID],'template','ROIs','ROItrans','bv','np');
if ~(evalin('base','exist(''ROIs'',''var'')') && evalin('base','exist(''template'',''var'')') &&  evalin('base','exist(''act_map'',''var'')'))
            disp('adata variables not found in workspace loading...')
            evalin('base',[' load(''' adata_dir mouse_ID '\' date_ID '\' file_ID ''')'])
end

% get the variables for saving old adata files
         if ~evalin('base','exist(''eye'',''var'')')
            eye=[];
            assignin('base', 'eye', eye)
        end
        if ~evalin('base','exist(''orientation'',''var'')')
            orientation=[];
            assignin('base', 'orientation', orientation)
        end
        
        if ~evalin('base','exist(''adata_dir'',''var'')')
            adata_dir = ImageAnalysis.Settings.AdataDir;
            assignin('base', 'adata_dir', adata_dir)
        end
         if ~evalin('base','exist(''data'',''var'')')
           disp('stack not loaded loading ...')
           loadExp(ExpIDs(get(dvh.exp_select,'value')));
        end
    evalin('base','find_cells_gui(template,ROIs,[0 0 0],act_map)');

function plot_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
%% plot it function
[~,ExpIDs,~,~,~,~]=get_menu_data(xls_num,xls_txt,dvh);
adata_dir = ImageAnalysis.Settings.AdataDir;
[file_ID,mouse_ID,date_ID]=find_adata_file(ExpIDs(get(dvh.exp_select,'value')),adata_dir);

if ~(evalin('base','exist(''ROIs'',''var'')') && evalin('base','exist(''template'',''var'')') &&  evalin('base','exist(''act_map'',''var'')'))
            disp('adata variables not found in workspace loading...')
            evalin('base',[' load(''' adata_dir mouse_ID '\' date_ID '\' file_ID ''')'])
end
evalin('base',['plot_activity_gen(ROIs,bv,np,template,''' ImageAnalysis.Settings.PlotFunction ''');']);

function register_entire_dir_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
answer=questdlg('are you sure you want to register all data in this directory? this takes roughly 10 min per exp');
if strcmp(answer,'Yes')
    [~,ExpIDs,~,same_site_as_ID]=get_menu_data(xls_num,xls_txt,dvh);
    disp('#############################################');
    disp('Now registering all data in current directory');
    disp('#############################################');
    for ind=1:length(ExpIDs)
        try
            disp('************************');
            disp(['Now registering exp ' num2str(ExpIDs(ind))]);
            disp('************************');
            evalin('base','clear data');
            loadData(ExpIDs(ind),adata_dir,1,0,1,same_site_as_ID(ind));
        catch
            disp(['There was an EORROR with Exp ' num2str(ExpIDs(ind))])
        end
    end
end

function register_multiple(e,h,xls_num,xls_txt,dvh,adata_dir)
ExpSerIDs=input('Which Experiments would you like to register (animal DB ID) [enter as vector]: ');
for knd=1:length(ExpSerIDs)
    %[~,ExpIDs,~,same_site_as_ID]=get_menu_data(xls_num,xls_txt,dvh);
    [ExpIDs,ExpIDs_ind]=unique(xls_num(xls_num(:,10)==ExpSerIDs(knd),7),'first');
    same_site_as_ID=xls_num(xls_num(:,10)==ExpSerIDs(knd),8);
    same_site_as_ID=same_site_as_ID(ExpIDs_ind);
    disp('#############################################');
    disp(['Now registering data of Experiment ' num2str(ExpSerIDs(knd))]);
    disp('#############################################');
    for ind=1:length(ExpIDs)
        %try
            disp('************************');
            disp(['Now registering exp ' num2str(ExpIDs(ind))]);
            disp('************************');
            evalin('base','clear data');
            loadData(ExpIDs(ind),adata_dir,1,0,1,same_site_as_ID(ind));
%         catch
%             disp(['There was an EORROR with Exp ' num2str(ExpIDs(ind))])
%         end
    end
end

function preview_adata_callback(e,h,xls_num,xls_txt,dvh,adata_dir)
[~,ExpIDs,~,same_site_as_ID]=get_menu_data(xls_num,xls_txt,dvh);
[file_ID,mouse_ID,date_ID]=find_adata_file(ExpIDs(get(dvh.exp_select,'value')),adata_dir);
if isempty(file_ID)
    disp('No AData found');
else
    disp(['Previewing Adata of file ' file_ID])
    prev_adata=load([adata_dir mouse_ID '\' date_ID '\' file_ID])
    assignin('base','prev_adata',prev_adata);
end

function [dateIDs,ExpIDs,ExpStrs,same_site_as_ID,mouseIDs,ExpGroup]=get_menu_data(xls_num,xls_txt,dvh)

[mouseIDs]=unique(xls_txt(2:end,9),'first');
mouse_ind=find(strcmp(xls_txt(2:end,9),mouseIDs(get(dvh.mouse_select,'value'))));
try
    [dateIDs,~,dateIDs_ind]=unique(datenum(xls_txt(mouse_ind+1,2)),'first'); % MS: date format string removed
catch
    [dateIDs,~,dateIDs_ind]=unique(datenum(xls_txt(mouse_ind+1,2),'dd/mm/yyyy'),'first');
end
dateIDs=datestr(dateIDs,'dd.mm.yyyy');
[ExpIDs,ExpIDs_ind]=unique(xls_num(mouse_ind(dateIDs_ind==get(dvh.date_select,'value')),7),'first');
same_site_as_ID=xls_num(mouse_ind(dateIDs_ind==get(dvh.date_select,'value')),8);
same_site_as_ID=same_site_as_ID(ExpIDs_ind);
ExpStrs=[num2str(ExpIDs), char(ones(length(ExpIDs),1)*[32,45,32]), num2str(same_site_as_ID)];
ExpStrs=mat2cell(ExpStrs,ones(length(ExpIDs),1));
mouse_IDs=(xls_txt(mouse_ind(dateIDs_ind==get(dvh.date_select,'value'))+1,6));

ExpGroup=[same_site_as_ID(get(dvh.exp_select,'value'));xls_num(xls_num(:,8)==same_site_as_ID(get(dvh.exp_select,'value')),7)];
[~,b,~]=unique(ExpGroup,'first');
ExpGroup=ExpGroup(sort(b));

for ind=1:length(ExpStrs)

        ExpStrs{ind}=[ExpStrs{ind} ' - ' mouse_IDs{ExpIDs_ind(ind)}];

end























